//
//  WEXVisitorView.swift
//  microBlog
//
//  Created by apple on 16/4/16.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol WEXVisitorViewDelegate: NSObjectProtocol{
    
    func loginButtonClick()
    func registeButtonClick()
    
}



class WEXVisitorView: UIView {
    
    weak var delegate:  WEXVisitorViewDelegate?
    
    // MARK: - 设置不同界面的数据内容
    func setVisitorInfo(imageName: String?, message: String?) {
        
        if let img = imageName, msg = message {
            
            houseView.image = UIImage(named: img)
            messageLabel.text = msg
            
        }else{
            
            setCircleAnim()
        }
        
    }
    
    
    // MARK: - 圆圈动画方法
    private func setCircleAnim() {
        //打开隐藏画面
        circle.hidden = false
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //滚动弧度
        anim.toValue = 2 * M_PI
        //重复次数
        anim.repeatCount = MAXFLOAT
        //旋转一圈需要的时间
        anim.duration = 20
        //切换后台是否需要停止动画
        anim.removedOnCompletion = false
        //加载动画
        circle.layer.addAnimation(anim, forKey: nil)
        
    }
    // MARK: - 加载界面
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    // MARK: - 初始化界面
    func setupUI() {
        
        backgroundColor = UIColor(white: 237 / 255.0 , alpha: 1)
        
        addSubview(circle)
        addSubview(backView)
        addSubview(houseView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //设置中间小房子的约束
        houseView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
            
        }
       
        //设置圆圈的约束
        circle.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.houseView.snp_centerX)
            make.centerY.equalTo(self.houseView.snp_centerY)
        }
        
        //遮罩背景
        backView.snp_makeConstraints { (make) -> Void in
           
            make.center.equalTo(self)
            
        }
        
        //提示文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.circle)
            make.top.equalTo(self.circle.snp_bottom).offset(10)
        }
        
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self.messageLabel.snp_leading)
            make.width.equalTo(100)
            make.top.equalTo(self.messageLabel.snp_bottom).offset(15)
            
        }
        registerButton.addTarget(self, action: "regisetButtonClick", forControlEvents: UIControlEvents.TouchUpInside)

        
        //登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.registerButton.snp_top)
            make.trailing.equalTo(self.messageLabel.snp_trailing)
            make.width.equalTo(100)
        }
        loginButton.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)

        
    }
    
    // MARK: - 实现按钮点击方法
    
    @objc private func regisetButtonClick(){
        
        delegate?.registeButtonClick()
        
    }
    
    @objc private func loginButtonClick(){
        
        delegate?.loginButtonClick()
        
    }
    // MARK: - 懒加载界面控件
    
    //中间的小房子
    private lazy var houseView: UIImageView = {
        
        let houseView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return houseView
    }()
    
    //圈界面
    private lazy var circle: UIImageView = {
        let circle = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        circle.hidden = true
        
        return circle
        
    }()
    
    //透明界面
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon" ))
    
    //文字界面
    private lazy var messageLabel: UILabel = {
        
        let messageLabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 16, preferredMaxLayoutWidth: 220)
        
        messageLabel.text = "世界这么大，可以去看看，微博这么精彩，可以来逛逛"
        
        messageLabel.textAlignment = .Center
        
        return messageLabel
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton.init(textColor: UIColor.orangeColor(), textFont: 14)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        return button
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton.init(textColor: UIColor.orangeColor(), textFont: 14)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        return button
        
    }()
    
    
}

// MARK: - 旧代码储备
extension WEXVisitorView {
    
    func oldLayoutDemo() {
        
        
        //        houseView.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        //        circle.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: circle, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: houseView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: circle, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: houseView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //        backView.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: houseView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: houseView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: circle, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        //        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: circle, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //        registerButton.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        //        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        //        loginButton.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        
    }
    
    
}
